package main

import (
	"context"
	"fmt"
	"github.com/pkg/errors"
	"golang.org/x/sync/errgroup"
	"net/http"
	_ "net/http/pprof"
	"os"
	"os/signal"
	"syscall"
)

func main() {
	g, ctx := errgroup.WithContext(context.Background())

	mux := http.NewServeMux()
	mux.HandleFunc("/ping", func(writer http.ResponseWriter, request *http.Request) {
		fmt.Fprintln(writer,"pong")
	})
	appServe := &http.Server{
		Addr: ":9091",
		Handler: mux,
	}

	pprofServe := &http.Server{
		Addr: ":9092",
		Handler: http.DefaultServeMux,
	}
	g.Go(func() error {
		return appServe.ListenAndServe()
	})
	g.Go(func() error {
		return pprofServe.ListenAndServe()
	})

	//处理signal信号
	chansignal := make(chan os.Signal,0)
	signal.Notify(chansignal,syscall.SIGINT,syscall.SIGTERM)

	g.Go(func() error {
		select {
		case <-chansignal:
			fmt.Println("信号中断")
		case <-ctx.Done():
			return errors.New("服务已暂停")
		}
		appServe.Shutdown(context.Background())
		pprofServe.Shutdown(context.Background())
		return nil
	})

	if err :=g.Wait(); err != nil {
		fmt.Println("err:", err)
	}

}





